﻿#include "OurController.h"
#include "SetupController.h"
#include "AddressController.h"

OurController::OurController(){}

OurController::~OurController(){}

void OurController::viewDidLoad(){
	CAView* view=this->getView();
	CAView* subview=CAView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(0, 400)), themeColor);
	CALabel* addr=CALabel::createWithLayout(DLayout(DHorizontalLayout_R_W(10, 200), DVerticalLayout_T_H(20, 40)));
	addr->setText("管理地址");
	addr->setFontSize(40);
	addr->setColor(CAColor4B::WHITE);
	addr->setTextAlignment(CATextAlignment::Right);
	addr->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
	addr->onTouchBegan([&](CATouch* touch, CAEvent* event){
		getRootWindow()->pushViewController(AddressController::create(), true);

		return true;
	});
	subview->addSubview(addr);
	CAImageView* portrait=CAImageView::createWithLayout(DLayout(DHorizontalLayout_L_W(10, 170), DVerticalLayout_H_C(170, 0.5)));
	portrait->setImage(CAImage::create("img/other.jpg"));
	subview->addSubview(portrait);
	CAImageView* maskView=CAImageView::createWithLayout(DLayout(DHorizontalLayout_L_W(10, 170), DVerticalLayout_H_C(170, 0.5)));
	maskView->setImage(CAImage::create("img/headbefore.png"));
	subview->addSubview(maskView);
	CALabel* name=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(190, 20), DVerticalLayout_H_C(40, 0.5)));
	name->setText(nickName);
	name->setFontSize(40);
	name->setColor(CAColor4B::WHITE);
	name->setTextAlignment(CATextAlignment::Left);
	name->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
	subview->addSubview(name);
	view->addSubview(subview);
	subview=CAView::createWithLayout(DLayout(DHorizontalLayout_L_R(20, 20), DVerticalLayout_T_H(320, 150)));
	subview->enabledLeftShadow(true);
	subview->enabledTopShadow(true);
	subview->enabledRightShadow(true);
	subview->enabledBottomShadow(true);
	CAImageView* img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(103, 0.17), DVerticalLayout_H_C(87, 0.35)));
	img->setImage(CAImage::create("img/bank.png"));
	subview->addSubview(img);
	CALabel* txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(160, 0.17), DVerticalLayout_H_C(40, 0.8)));
	txt->setFontSize(30);
	txt->setText("银行卡管理");
	txt->setColor(ccc4(51, 51, 51, 255));
	subview->addSubview(txt);
	img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(118, 0.5), DVerticalLayout_H_C(88, 0.35)));
	img->setImage(CAImage::create("img/balance.png"));
	subview->addSubview(img);
	txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(130, 0.5), DVerticalLayout_H_C(40, 0.8)));
	txt->setFontSize(30);
	txt->setText("余额管理");
	txt->setColor(ccc4(51, 51, 51, 255));
	subview->addSubview(txt);
	img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(86, 0.85), DVerticalLayout_H_C(88, 0.35)));
	img->setImage(CAImage::create("img/order.png"));
	subview->addSubview(img);
	txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(130, 0.85), DVerticalLayout_H_C(40, 0.8)));
	txt->setFontSize(30);
	txt->setText("订单管理");
	txt->setColor(ccc4(51, 51, 51, 255));
	subview->addSubview(txt);
	view->addSubview(subview);
	subview=CAView::createWithLayout(DLayout(DHorizontalLayout_L_R(20, 20), DVerticalLayout_T_H(500, 180)));
	subview->enabledLeftShadow(true);
	subview->enabledTopShadow(true);
	subview->enabledRightShadow(true);
	subview->enabledBottomShadow(true);
	img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(52, 0.15), DVerticalLayout_H_C(52, 0.35)));
	img->setImage(CAImage::create("img/personal.png"));
	subview->addSubview(img);
	txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(130, 0.15), DVerticalLayout_H_C(40, 0.7)));
	txt->setFontSize(30);
	txt->setText("个人认证");
	txt->setColor(ccc4(51, 51, 51, 255));
	subview->addSubview(txt);
	img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(56, 0.38), DVerticalLayout_H_C(47, 0.35)));
	img->setImage(CAImage::create("img/enterprisecertification.png"));
	subview->addSubview(img);
	txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(130, 0.38), DVerticalLayout_H_C(40, 0.7)));
	txt->setFontSize(30);
	txt->setText("企业认证");
	txt->setColor(ccc4(51, 51, 51, 255));
	subview->addSubview(txt);
	img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(48, 0.61), DVerticalLayout_H_C(47, 0.35)));
	img->setImage(CAImage::create("img/vehiclemanagement.png"));
	subview->addSubview(img);
	txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(130, 0.61), DVerticalLayout_H_C(40, 0.7)));
	txt->setFontSize(30);
	txt->setText("车辆管理");
	txt->setColor(ccc4(51, 51, 51, 255));
	subview->addSubview(txt);
	img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(51, 0.85), DVerticalLayout_H_C(47, 0.35)));
	img->setImage(CAImage::create("img/storemanagement.png"));
	subview->addSubview(img);
	txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(130, 0.85), DVerticalLayout_H_C(40, 0.7)));
	txt->setFontSize(30);
	txt->setText("我要开店");
	txt->setColor(ccc4(51, 51, 51, 255));
	subview->addSubview(txt);
	view->addSubview(subview);
	subview=CAView::createWithLayout(DLayout(DHorizontalLayout_L_R(20, 20), DVerticalLayout_T_H(700, 320)));
	subview->enabledLeftShadow(true);
	subview->enabledTopShadow(true);
	subview->enabledRightShadow(true);
	subview->enabledBottomShadow(true);
	table=CATableView::createWithLayout(DLayoutFill);
	table->setAllowsSelection(true);
	table->setAllowsMultipleSelection(false);
	table->setShowsScrollIndicators(false);
	table->setSeparatorViewHeight(0);
	subview->addSubview(table);
	table->onNumberOfRowsAtIndexPath([&](unsigned int section){
		return datas.size();
	});
	table->onCellHeightAtIndexPath([](unsigned int section, unsigned int row){
		return 80;
	});
	table->onCellAtIndexPath([&](DSize cellSize, unsigned int section, unsigned int row){
		CATableViewCell* cell=dynamic_cast<CATableViewCell*>(table->dequeueReusableCellWithIdentifier("our_cell"));
		if(cell==nullptr){
			cell=CATableViewCell::create("our_cell");
			CAView* subview=cell->getContentView();

			CAImageView* icon=CAImageView::create();
			icon->setTag(100);
			subview->addSubview(icon);

			CALabel* name=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_W(50, 300), DVerticalLayout_H_C(35, 0.5)));
			name->setTag(200);
			name->setFontSize(35);
			name->setColor(ccc4(51, 51, 51, 255));
			name->setTextAlignment(CATextAlignment::Left);
			name->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
			subview->addSubview(name);

			CAImageView* advance=CAImageView::createWithLayout(DLayout(DHorizontalLayout_R_W(20, 16), DVerticalLayout_H_C(28, 0.5)));
			advance->setTag(300);
			advance->setImage(CAImage::create("img/advance.png"));
			subview->addSubview(advance);
		}
		cell->setDraggingLength(0);

		CSJson::Value val=datas[row];
		CAView* subview=cell->getContentView();
		CAImageView* icon=(CAImageView*)subview->getSubviewByTag(100);
		icon->setImage(CAImage::create(val["icon"].asCString()));
		icon->setLayout(DLayout(DHorizontalLayout_L_W(10, val["width"].asInt()), DVerticalLayout_H_C(val["height"].asInt(), 0.5)));

		CALabel* name=(CALabel*)subview->getSubviewByTag(200);
		name->setText(val["name"].asCString());

		return cell;
	});
	table->onDidSelectCellAtIndexPath([](unsigned int section, unsigned int row){
		getRootWindow()->pushViewController(SetupController::create(), true);
	});
	view->addSubview(subview);

	this->fillData();
}

void OurController::viewDidAppear(){
	//解决编辑地址时跳转的BUG
	getRootWindow()->setNavigationBarHidden(true, false);
}

void OurController::fillData(){
	CSJson::Value obj1;
	obj1["icon"]="img/attention.png";
	obj1["width"]=32;
	obj1["height"]=28;
	obj1["name"]="我的关注";
	datas.append(obj1);
	CSJson::Value obj2;
	obj2["icon"]="img/release.png";
	obj2["width"]=27;
	obj2["height"]=28;
	obj2["name"]="我的需求发布";
	datas.append(obj2);
	CSJson::Value obj3;
	obj3["icon"]="img/customer.png";
	obj3["width"]=30;
	obj3["height"]=27;
	obj3["name"]="联系客服";
	datas.append(obj3);
	CSJson::Value obj4;
	obj4["icon"]="img/setup.png";
	obj4["width"]=32;
	obj4["height"]=28;
	obj4["name"]="系统设置";
	datas.append(obj4);
	
	table->reloadData();
}